package com.probox.modules.controller.platfom;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.HttpUtil;
import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.domain.platfom.PlatformSmsConfig;
import com.probox.common.entity.service.platfom.IPlatformSmsConfigService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 短信配置Controller
 *
 * @date 2021-06-22
 */
@RestController
@RequestMapping("/platform/config")
public class PlatformSmsConfigController extends BaseController {
    @Autowired
    private IPlatformSmsConfigService platformSmsConfigService;

    /**
     * 查询短信配置列表
     */
    @PreAuthorize(hasPermi = "platform:config:list")
    @GetMapping("/list")
    public TableDataInfo list(PlatformSmsConfig platformSmsConfig) {
        startPage();
        List<PlatformSmsConfig> list = platformSmsConfigService.queryList(platformSmsConfig);
        return getDataTable(list);
    }

    /**
     * 导出短信配置列表
     */
    @PreAuthorize(hasPermi = "platform:config:export")
    @Log(title = "短信配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlatformSmsConfig platformSmsConfig) throws IOException {
        List<PlatformSmsConfig> list = platformSmsConfigService.queryList(platformSmsConfig);
        ExcelUtil<PlatformSmsConfig> util = new ExcelUtil<PlatformSmsConfig>(PlatformSmsConfig.class);
        util.exportExcel(response, list, "短信配置数据");
    }

    /**
     * 获取短信配置详细信息
     */
    @PreAuthorize(hasPermi = "platform:config:query")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId, javax.servlet.http.HttpServletRequest request) {
        return AjaxResult.success(platformSmsConfigService.selectPlatformSmsConfigById(configId));
    }

    /**
     * 获取验证码
     *
     * @param phone
     * @param request
     * @return
     */
    @GetMapping(value = "/phoneCode")
    public AjaxResult getPhoneCode(@RequestParam("phone") String phone, javax.servlet.http.HttpServletRequest request) {
        if (ObjectUtil.isEmpty(phone)) {
            throw new BaseException("手机号码不能为空！");
        }
        platformSmsConfigService.smsLoginCode(phone, HttpUtil.getIp(request), 1L);
        return AjaxResult.success();
    }

    /**
     * 新增短信配置
     */
    @PreAuthorize(hasPermi = "platform:config:add")
    @Log(title = "短信配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlatformSmsConfig platformSmsConfig) {
        return toAjax(platformSmsConfigService.insertPlatformSmsConfig(platformSmsConfig));
    }

    /**
     * 修改短信配置
     */
    @PreAuthorize(hasPermi = "platform:config:edit")
    @Log(title = "短信配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformSmsConfig platformSmsConfig) {
        return toAjax(platformSmsConfigService.updatePlatformSmsConfig(platformSmsConfig));
    }

    /**
     * 删除短信配置
     */
    @PreAuthorize(hasPermi = "platform:config:remove")
    @Log(title = "短信配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        return toAjax(platformSmsConfigService.deletePlatformSmsConfigByIds(configIds));
    }


}
